home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d17 / pgraf110.arc / DEMO.C < prev    next >
Text File  |  1991-06-21  |  8KB  |  250 lines

  1. /********************************************************************
  2.  *                                                                  *
  3.  *  "Printer Graphics Interface" Demonstration Program              *
  4.  *                                                                  *
  5.  *  This program demonstrates how to use various functions          *
  6.  *  available in the PGRAPH library.                                *
  7.  *                                                                  *
  8.  *  Author: F van der Hulst                                         *
  9.  *                                                                  *
  10.  * Revisions:                                                       *
  11.  * 27 March 1991: Initial release (Turbo C v2.0 only)               *
  12.  * 07 April 1991: Ported to MicroSoft C v5.1                        *
  13.  *                                                                  *
  14.  ********************************************************************/
  15.  
  16. #include <io.h>
  17. #include <fcntl.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <conio.h>
  22.  
  23. #if defined(__HUGE__) || defined(__LARGE__) || defined(__MEDIUM__)
  24. #ifdef __TURBOC__
  25. #include <graphics.h>
  26. #include <bgidrive.h>
  27. #else
  28. #include <graph.h>
  29. #endif
  30. #else
  31. #include <process.h>
  32. #endif
  33.  
  34. #include "pgraph.h"
  35.  
  36. void shapes_demo(void);
  37. void stroked_fonts_demo(void);
  38. void default_font_demo(void);
  39. void horiz_text_demo(void);
  40. void vert_text_demo(void);
  41. void text_scaling_demo(void);
  42. void flood_fill_demo(void);
  43. void lines_demo(void);
  44. void shape_fill_demo(void);
  45. void pie_demo(void);
  46.  
  47. #if defined(__HUGE__) || defined(__LARGE__) || defined(__MEDIUM__)
  48. void image_demo(void);
  49. void view_demo(void);
  50. #endif
  51.  
  52. #if !defined(__TINY__)
  53. void LASERJET_DRIVER(void);            /* PGI Object entries */
  54. extern int far _Cdecl STAR_DRIVER[];
  55. #endif
  56.  
  57. #define MAX_WIDTH 801            /* Maximum width of any PGRAPH viewport defined in the program */
  58.  
  59. int screen_echo = 0;                /* Echo printer output to screen or not */
  60. int page_height, page_width;    /* Size of page in pixels */
  61. int prn;                                /* Output device */
  62.  
  63. struct {                                /* Page sizes in 1/100 of an inch available on various printers */
  64.     int x;
  65.     int y;
  66. } page_size[] = {
  67.     {0, 0},
  68.     {800, 1100},                    /* STAR */
  69.     {780, 1088},               /* LaserJet */
  70.     {800, 1100},                    /* Epson LX400 */
  71.     {800, 1100},                    /* USER1 */
  72.     {800, 1100} };                    /* USER2 */
  73.  
  74.  
  75. /********************************************************************
  76.  Process command line arguments. */
  77.  
  78. void get_args(int argc, char *argv[], int *driver, int *mode, char *dev_name, char *demos)
  79. {
  80. int i, low, high;
  81.  
  82.     for (i = 1; i < argc; i++) {
  83.         strupr(argv[i]);
  84.         if (argv[i][0] != '/' && argv[i][0] != '-') {
  85.             printf("Invalid command line switch: %s\n(Must start with '-' or '/')\n", argv[i]);
  86.             exit(1);
  87.         }
  88.         if (argv[i][1] == '?' || argv[i][1] == 'H') {
  89.             printf("Command syntax: %s [/O=outputdevice][/P=printer][/M=mode][/D=demos]\n\n", argv[0]);
  90.             printf("outputdevice may be PRN, or a filename\n");
  91.             printf("printer may be STAR, LASERJET, LX-400, USER1, or USER2\n");
  92.             printf("\tIf you use LX-400, USER1, USER2, the corresponding .PGI file\n\tmust be in the current directory\n");
  93.             printf("mode is an integer in the range 0 to the maximum mode for the selected printer\n");
  94.             printf("demos is a series of letters, identifying which demos to print\n");
  95.             printf("\nDefault values are PRN and STAR, and a mode better than 120dpi)\n\n");
  96.             exit(0);
  97.         }
  98.         if (argv[i][2] != '=') {
  99.             printf("Invalid command line switch: %s\n(Must be /%c=VALUE)\n", &argv[i][1]);
  100.             exit(1);
  101.         }
  102.         switch(argv[i][1]) {
  103.         case 'O':
  104.         case 'o':
  105.             strcpy(dev_name, &argv[i][3]);
  106.             break;
  107.         case 'P':
  108.         case 'p':
  109.             if (strcmp(&argv[i][3], "STAR") == 0)                *driver = STAR;
  110.             else if (strcmp(&argv[i][3], "LX-400") == 0)         *driver = LX400;
  111.             else if (strcmp(&argv[i][3], "LASERJET") == 0)     *driver = LASERJET;
  112.             else if (strcmp(&argv[i][3], "USER1") == 0)         *driver = USER1;
  113.             else if (strcmp(&argv[i][3], "USER2") == 0)         *driver = USER2;
  114.             else {
  115.                 printf("Unknown printer type: %s\n", &argv[i][3]);
  116.                 exit(1);
  117.             }
  118.             break;
  119.         case 'M':
  120.         case 'm':
  121.             *mode = atoi(&argv[i][3]);
  122.             p_getmoderange(*driver, &low, &high);
  123.             if (*mode > high || *mode < low) {
  124.                 printf("Invalid mode: %d\n", *mode);
  125.                 exit(1);
  126.             }
  127.             break;
  128.         case 'D':
  129.         case 'd':
  130.             strcpy(demos, &argv[i][3]);
  131.             break;
  132.         default:
  133.             printf("Invalid command line switch: %s\n(Must be /D, /O, /P, or /M)\n", argv[i]);
  134.             exit(1);
  135.         }
  136.     }
  137. }
  138.  
  139. /********************************************************************
  140.  Find the best mode (the worst X resolution that will display
  141.  MAX_WIDTH bits) for the selected printer. */
  142.  
  143. int best_mode(void)
  144. {
  145. int i;
  146. int xres, yres, best_x, best_y;
  147. int mode;
  148.  
  149.     mode = 0;
  150.     best_y = best_x = 1000;
  151.     for (i = 0; i <= p_getmaxmode(); i++) {
  152.         p_setgraphmode(i);
  153.         p_getresolution(&xres, &yres);
  154.         if ((long)xres * page_width / 100 >= MAX_WIDTH+7)
  155.             if ((xres < best_x) || (xres == best_x && yres < best_y)) {
  156.                 best_y = yres;
  157.                 best_x = xres;
  158.                 mode = i;
  159.             }
  160.     }
  161.     return mode;
  162. }
  163.  
  164. void main(int argc, char *argv[])
  165. {
  166. int driver = 1, mode = -1, dummy_mode, errorcode;
  167. int xres, yres;
  168. static char filename[80] = "PRN";
  169. static char selection[] = "ABCDEFGHIJKL";
  170. #if !defined(__TINY__)
  171.     if (p_registerfarbgidriver(STAR_DRIVER) < 0) {
  172.         printf("Couldn't register STAR\n");
  173.         return;
  174.     }
  175.     if (p_registerbgidriver(LASERJET_DRIVER) < 0) {
  176.         printf("Couldn't register LASERJET\n");
  177.         return;
  178.     }
  179. #endif
  180.     get_args(argc, argv, &driver, &mode, filename, selection);
  181.     if (strcmp(filename, "PRN") != 0) {
  182.         prn = open(filename, O_WRONLY | O_BINARY);
  183.         if (prn == NULL) {
  184.             printf("Couldn't open %s for output\n", filename);
  185.             exit(1);
  186.         }
  187.     } else prn = fileno(stdprn);
  188.  
  189.     dummy_mode = mode == -1 ? 0 : mode;
  190.     p_initgraph(&driver, &dummy_mode, NULL);
  191.     errorcode = p_graphresult();             /* preserve error return */
  192.     if (errorcode != grOk) {                    /* error? */
  193.         printf("Graphics error: %d\n", errorcode);
  194.         return;
  195.     }
  196.     page_height = page_size[driver].y;
  197.     page_width = page_size[driver].x;
  198.     if (mode < 0 || mode > p_getmaxmode())        mode = best_mode();
  199.     p_setgraphmode(mode);
  200.     p_getresolution(&xres, &yres);
  201.     printf("Currently set to mode %d (%d by %ddpi).\n", mode, xres, yres);
  202.     if (strchr(selection, 'A') != NULL) shapes_demo();
  203.     if (strchr(selection, 'B') != NULL) stroked_fonts_demo();
  204.     if (strchr(selection, 'C') != NULL) default_font_demo();
  205.     if (strchr(selection, 'D') != NULL) horiz_text_demo();
  206.     if (strchr(selection, 'E') != NULL) vert_text_demo();
  207.     if (strchr(selection, 'F') != NULL) text_scaling_demo();
  208.     if (strchr(selection, 'G') != NULL) shape_fill_demo();
  209.     if (strchr(selection, 'H') != NULL) flood_fill_demo();
  210.     if (strchr(selection, 'I') != NULL) lines_demo();
  211.     if (strchr(selection, 'J') != NULL) pie_demo();
  212. #if defined(__HUGE__) || defined(__LARGE__) || defined(__MEDIUM__)
  213.     if (strchr(selection, 'K') != NULL) image_demo();
  214.     if (strchr(selection, 'L') != NULL) view_demo();
  215.     p_closegraph();
  216.  
  217. #else
  218.     p_closegraph();
  219.     if (strchr(selection, 'K') != strchr(selection, 'L'))
  220.         execv("DEMO_GR", argv);
  221. #endif
  222.     if (strcmp(filename, "PRN") != 0)    close(prn);
  223. }
  224.  
  225. /********************************************************************
  226.  End of outputting a slice to the buffer. Check to see whether it will
  227.  fit on the current page. If not skip to the top of the next page */
  228.  
  229. void end_slice(void)
  230. {
  231. int xres, yres;
  232. int height;
  233. static int line_num = 0;
  234. static char FF = 0x0c;
  235.  
  236.     p_getresolution(&xres, &yres);
  237.     height = p_getmaxy() + 1;
  238.     line_num += height;
  239.     if (line_num > (long)yres * page_height / 100) {
  240.         line_num = height;
  241.         write(prn, &FF, 1);
  242.     }
  243.  
  244.     if (!screen_echo)    printf("Printing...");
  245.     p_print(prn);
  246.     if (!screen_echo) printf("\n");
  247.     p_cleardevice();
  248. }
  249.  
  250.